﻿using BPMS.Business;
using BPMS.Entity;
using BPMS.IBusiness;
using DotNet.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BPMS.MVC.Controllers
{
    public class TelController : Controller
    {
        BPMS_PersonIBLL bpms_personibll = new BPMS_PersonBLL();
        BPMS_Person bpms_person = new BPMS_Person();

        #region 通讯录管理
        /// <summary>
        /// 通讯录管理
        /// </summary>
        /// <returns></returns>
        public ActionResult TelIndex()
        {   
            return View();
        }
        #endregion

        #region 通讯录列表
        /// <summary>
        /// 通讯录列表
        /// </summary>
        /// <returns></returns>
        public ActionResult TelList()
        {
            return View();
        }

        public void LoadAction()
        {
            string active = Request["action"];                                          //提交类型
            int pageIndex = CommonHelper.GetInt(Request["pqGrid_PageIndex"]);           //当前页
            int pageSize = CommonHelper.GetInt(Request["pqGrid_PageSize"]);             //当前页大小
            string orderField = Request["pqGrid_OrderField"];                           //排序字段  
            string orderType = Request["pqGrid_OrderType"];                             //排序类型
            string pqGrid_Sort = Request["pqGrid_Sort"];                                //要显示字段
            string key = Request["key"];                                                //主键
            string Category = Request["Category"];                                      //机构分类
            string OrganizationId = Request["OrganizationId"];                          //机构主键
            string Parm_Key_Value = Request["Parm_Key_Value"];                          //搜索条件键值
            string search = Request["search"];                                          //模糊查询条件
            switch (active)
            {
                case "GridBindList"://加载列表
                    int count = 0;
                    Hashtable parm = HashtableHelper.String_Key_ValueToHashtable(Parm_Key_Value);
                    parm["Category"] = Category;
                    parm["OrganizationId"] = OrganizationId;
                    Response.Write(JsonHelper.PqGridPageJson<BPMS_Person>(bpms_personibll.GetPageList(parm, orderField, orderType, pageIndex, pageSize, ref count), pageIndex, pqGrid_Sort, count));
                    Response.End();
                    break;
                case "Delete":    //删除数据
                    Response.Write(bpms_personibll.Delete(key));
                    Response.End();
                    break;
                case "autocomplete"://员工自动补全，返回JSON
                    Response.Write(JsonHelper.ListToJson<BPMS_Person>(bpms_personibll.autocomplete(search), "JSON"));
                    Response.End();
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 通讯录表单
        /// <summary>
        /// 职员表单
        /// </summary>
        /// <returns></returns>
        public ActionResult TelForm()
        {
            return View();
        }
        /// <summary>
        /// 确认事件
        /// </summary>
        /// <param name="bpms_person">职员信息</param>
        /// <returns></returns>
        public ActionResult AcceptClick(BPMS_Person bpms_person)
        {
            string key = Request["key"];                                                //主键
            bool IsOk = false;
            bpms_person.Spell = PinyinHelper.PinyinString(bpms_person.RealName);
            if (!string.IsNullOrEmpty(key))//判断是否编辑
            {
                bpms_person.PerId = key;
                bpms_person.ModifyDate = DateTime.Now;
                bpms_person.ModifyUserId = SessionHelper.GetSessionUser().UserId;
                IsOk = bpms_personibll.Update(bpms_person);
            }
            else
            {
                bpms_person.PerId = CommonHelper.GetGuid;
                bpms_person.ModifyDate = DateTime.Now;
                bpms_person.ModifyUserId = SessionHelper.GetSessionUser().UserId;
                IsOk = bpms_personibll.Insert(bpms_person);
            }
            return Content(IsOk.ToString().ToLower());
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        public ActionResult InitControl(string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                bpms_person = bpms_personibll.GetEntity(key);
                return Content(JsonHelper.ObjectToJson(bpms_person));
            }
            return null;
        }
        /// <summary>
        /// 初始化绑定下拉框 数据字典
        /// </summary>
        public ActionResult InitBindDrop()
        {
            BPMS_ItemDetailsIBLL bpms_itemdetailsibll = new BPMS_ItemDetailsBLL();
            return Content(JsonHelper.DropToJson<BPMS_ItemDetails>(bpms_itemdetailsibll.GetList(), "JSON"));
        }
        #endregion
    }
   
}
